package com.geping.etl.common.util;

import com.geping.etl.utils.EnvUitl;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class JDBCUtils {
	private static String driver = null;
	private static String url = null;
	private static String user = null;
	private static String pwd = null;// oracle数据库的用户密码
	private static PreparedStatement sta = null;
	private static ResultSet rs = null;
	private static Connection conn = null;

	public static Connection getConnection(){ // 1.加载驱动程序
		Properties props = new Properties();
		try {
			//读取外置文件
			if(Constants.ON.equals(EnvUitl.outSetting)){
				String path = EnvUitl.outSettingPath + EnvUitl.outSettingConfigName;
				FileInputStream ins = new FileInputStream(path);
				props.load(ins);
			}else {
				props.load(JDBCUtils.class.getClassLoader().getResourceAsStream(EnvUitl.profile + "/config.properties"));
			}
			String databaseType = props.getProperty("database.type");
			driver = props.getProperty(databaseType+"driverClassName");//数据库驱动
			url = props.getProperty(databaseType+"url");//数据库
			user = props.getProperty(databaseType+"username");//用户名
			pwd = props.getProperty(databaseType+"password");//密码

			Class.forName(driver);
			// 2.建立与数据库的连接
			if (conn == null || conn.isClosed()) {
				conn = DriverManager.getConnection(url, user, pwd);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	/**
	 * * @param sql * sql语句 修改 *  @return
	 */
	public static int update(Connection conn,String sql) {
		int count = 0;
		try {
			sta = conn.prepareStatement(sql);
			count = sta.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close();
		}
		return count;
	}

	/**
	 *
	 * * @param sql sql语句 * 
	 */

	public static ResultSet Query(Connection conn,String sql) {
		try {
			sta = conn.prepareStatement(sql);
			rs = sta.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
		}
		return rs;
	}


	/** * 关闭资源 */
	public static void close() {
		try {
			if (rs != null) {
				rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (sta != null) {
					sta.close();
				}
			} catch (SQLException e2) {
				e2.printStackTrace();
			} finally {
				if (conn != null) {
					try {
						conn.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	/**
	 *
	 * * @param sql sql语句 *
	 */

	public ResultSet query(Connection conn,PreparedStatement ps,ResultSet rs,String sql) {
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}

	/** * 关闭资源 */
	public void close(ResultSet rs,PreparedStatement ps,Connection conn) {
		try {
			if (rs != null) {
				rs.close();
			}

			if (ps != null) {
				ps.close();
			}

			if (conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
